package com.example.vendingmachine.service.impl;

import com.example.vendingmachine.dto.PointDTO;
import com.example.vendingmachine.dto.PointQuery;
import com.example.vendingmachine.entity.Area;
import com.example.vendingmachine.entity.Point;
import com.example.vendingmachine.mapper.PointMapper;
import com.example.vendingmachine.service.AreaService;
import com.example.vendingmachine.service.PointService;
import com.example.vendingmachine.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 点位服务实现类
 */
@Service
@RequiredArgsConstructor
public class PointServiceImpl implements PointService {

    private final PointMapper pointMapper;
    private final AreaService areaService;

    @Override
    public PageInfo<Point> listPoints(PointQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return new PageInfo<>(pointMapper.selectList(query));
    }

    @Override
    public Point getPointById(Long id) {
        Point point = pointMapper.selectById(id);
        if (point == null) {
            throw new BusinessException("点位不存在");
        }
        return point;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createPoint(PointDTO pointDTO) {
        // 检查区域是否存在且状态为启用
        checkArea(pointDTO.getAreaId());

        Point point = new Point();
        BeanUtils.copyProperties(pointDTO, point);
        // 设置状态为启用
        point.setStatus("ENABLED");

        pointMapper.insert(point);
        return point.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePoint(PointDTO pointDTO) {
        // 检查点位是否存在
        Point existPoint = getPointById(pointDTO.getId());
        
        // 如果更换了区域
        if (!existPoint.getAreaId().equals(pointDTO.getAreaId())) {
            // 检查新区域
            checkArea(pointDTO.getAreaId());
        }

        Point point = new Point();
        BeanUtils.copyProperties(pointDTO, point);
        pointMapper.update(point);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePoint(Long id) {
        // 检查点位是否存在
        Point point = getPointById(id);
        // 检查点位下是否有设备
        if (pointMapper.countMachinesByPointId(id) > 0) {
            throw new BusinessException("点位下存在设备，无法删除");
        }
        pointMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePointStatus(Long id, String status) {
        // 检查点位是否存在
        getPointById(id);
        pointMapper.updateStatus(id, status);
    }

    @Override
    public boolean hasPointsByAreaId(Long areaId) {
        return pointMapper.countByAreaId(areaId) > 0;
    }

    /**
     * 检查区域是否存在且状态为启用
     */
    private void checkArea(Long areaId) {
        Area area = areaService.getAreaById(areaId);
        if (!"ENABLED".equals(area.getStatus())) {
            throw new BusinessException("所选区域已被禁用");
        }
    }
} 